
/**  
* @Title: RedisUtils.java  
* @Package org.sapling.data.utils  
* @Description: TODO
* @author DcfZero  
* @date 2021年6月13日  
* @version V1.0  
*/ 
package org.sapling.data.utils;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

/**  
* TODO
* @ClassName: RedisUtils  
* @author DcfZero 
* @date 2021年6月13日  
*    
*/
@Component
@ConditionalOnProperty(name = {"system.enabled-redis-cache"}, havingValue = "true")
public class RedisUtils {

	@Autowired
	private RedisTemplate redisTemplate;
	
	public void set(String key,Object value) throws Exception{
		ValueOperations<String, Object> operations = redisTemplate.opsForValue();
		operations.set(key, value);
	}
	
	
	public void set(String key,Object value,Long timeOut,TimeUnit timeUnit)throws Exception {
		ValueOperations<String, Object> operations = redisTemplate.opsForValue();
		operations.set(key, value);
		redisTemplate.expire(key, timeOut, timeUnit);
	}
	
	public Object get(String key) {
		ValueOperations<String, Object> operations = redisTemplate.opsForValue();
		return operations.get(key);
	}
	
	public void remove(String... keys) {
		for(String key : keys) {
			remove(key);
		}
	}
	
	public void remove(String key) {
		if(exists(key)) {
			redisTemplate.delete(key);
		}
	}
	
	public boolean exists(String key) {
		return redisTemplate.hasKey(key);
	}
	
}
